/*
 *   ========================================================
 *   Copyright(c) 2012 杭州龙骞科技-版权所有
 *   ========================================================
 *   本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 *   任何形式复制代码的部分或全部, 并以任何形式传播。
 *   公司网址
 *
 *   			http://www.hzdracom.com/
 *
 *   ========================================================
 */

package com.shanwen.shopping.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.classic.core.utils.NetworkUtil;
import com.classic.core.utils.ToastUtil;
import com.shanwen.shopping.R;
import com.shanwen.shopping.consts.Consts;

import butterknife.BindView;

public class WebViewActivity extends AppBaseActivity {
    public static final String PARAMS_TITLE = "title";
    public static final String PARAMS_URL = "url";

    @BindView(R.id.common_webview) WebView webView;
    ProgressBar progressBar;
    private String title;
    private String url;

    public static final void start(Activity activity, String title, String url) {
        if (null != activity && !TextUtils.isEmpty(url)) {
            Intent intent = new Intent(activity, WebViewActivity.class);
            intent.putExtra(PARAMS_TITLE, title);
            intent.putExtra(PARAMS_URL, url);
            activity.startActivity(intent);
        }
    }

    @Override public int getLayoutResId() {
        return R.layout.common_webview;
    }

    @Override public void initData() {
        super.initData();
        if (getIntent().hasExtra(PARAMS_TITLE)) {
            title = getIntent().getStringExtra(PARAMS_TITLE);
        }
        if (getIntent().hasExtra(PARAMS_URL)) {
            url = getIntent().getStringExtra(PARAMS_URL);
        }
        if (TextUtils.isEmpty(url)) {
            finish();
            return;
        }
    }

    @SuppressLint("AddJavascriptInterface") @Override public void initView() {
        super.initView();
        progressBar = (ProgressBar) findViewById(R.id.common_webview_progressbar);
        setTitle(TextUtils.isEmpty(title) ? "" : title);
        setBottomLineEnabled(false);
        setBackEnabled(true);
        webView.requestFocusFromTouch();
        webView.requestFocus();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);//适应屏幕
        webView.getSettings().setSupportZoom(false);//缩放
        // 让网页自适应屏幕宽度
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        // 设置缓存
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.setWebViewClient(new TrainingWebViewClient());
        webView.setWebChromeClient(new TrainingWebChromeClient());

        if(NetworkUtil.isNetworkAvailable(activity)){
            webView.loadUrl(url);
        }else{
            ToastUtil.showToast(activity.getApplicationContext(),"网络异常,请检查网络连接");
        }
    }

    @Override public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

    @Override protected void onDestroy() {
        if (webView != null) {
            webView.clearFormData();
            webView.clearHistory();
            webView.clearMatches();
            webView.clearCache(true);
            webView.destroy();
        }
        super.onDestroy();
    }

    private final class TrainingWebChromeClient extends WebChromeClient{
        @Override public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress < Consts.MAX_PROGRESS && progressBar.getVisibility() == View.GONE) {
                progressBar.setVisibility(View.VISIBLE);
            }
            progressBar.setProgress(newProgress);
            super.onProgressChanged(view, newProgress);
        }
    }

    private final class TrainingWebViewClient extends WebViewClient{
        @Override public void onPageFinished(final WebView view, String url) {
            super.onPageFinished(view, url);
//            new Handler().postDelayed(new Runnable() {
//                @Override public void run() {
                    progressBar.setVisibility(View.GONE);
//                }
//            }, Consts.DEFAULT_DELAYED);
        }
    }
}
